package com.test.lab.validate;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

public class ValidateUtil {

    /**
     * 校验规则map
     */
    private Map<String, Rule[]> ruleMap = null;

    /**
     * 参数名字map
     */
    private Map<String, String> nameMap = null;

    /**
     * 参数值map
     */
    private Map<String, String> valueMap = null;

    //TODO
    private static final Pattern PATTERN_TELEPHONE = Pattern.compile("1[3|4|5|7|8|][0-9]{9}]");

    /**
     * 无参构造方法
     */
    public ValidateUtil() {
        ruleMap = new HashMap<String,Rule[]>();
        nameMap = new HashMap<String, String>();
        valueMap = new HashMap<String, String>();
    }

    /**
     * 添加需要校验的参数和参数规则
     * @param key ruleMap,nameMap,valueMap的共同Key
     * @param value 要校验的参数
     * @param name 参数名字
     * @param ruleArr 校验规则数组
     */
    public void add(String key, String value, String name, Rule[] ruleArr) {
        nameMap.put(key, name);
        valueMap.put(key, value);
        ruleMap.put(key, ruleArr);
    }

    /**
     * 校验所添加到的所有参数
     * @return 校验结果
     */
    public String validateString() {
        String message = null;
        Map<String, Object> resultMap = null;
        for (Entry<String, Rule[]> entry : ruleMap.entrySet()) {// 循环校验规则

            // 获取每一个校验参数的key
            String key = entry.getKey();
            resultMap = validate(key);
            boolean result = (Boolean)resultMap.get("result");
            if (!result) {
                message = (String)resultMap.get("message");
                break;
            }
        }
        this.clear();
        return message;
    }

    /**
     * 验证单个参数是否符合校验规则
     * @param key map的某个key
     * @return 参数校验结果
     */
    private Map<String, Object> validate(String key) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("result", Boolean.TRUE);

        // 获取某个参数所有的校验规则并校验
        Rule[] rules = ruleMap.get(key);
        if (rules != null && rules.length > 0) {
            for (Rule rule : rules) {
                rule.setValue(valueMap.get(key));
                if (!rule.valid()) {
                    resultMap.put("result", Boolean.FALSE);
                    resultMap.put("message", nameMap.get(key) + rule.getMessage());
                    break;
                }
            }
        }
        return resultMap;
    }

    /**
     * 清空 ruleMap，nameMap，valueMap
     * @author LiShiChong
     * @date 2015年9月21日 上午9:50:07
     */
    private void clear() {
        ruleMap = null;
        nameMap = null;
        valueMap = null;
    }

    public static void main(String[] args) {
        ValidateUtil vu = new ValidateUtil();

        //用户名校验
        vu.add("userName", "15757182251","userName",
                new Rule[]{new Required(), new Length(11), new Numeric(), new Regex(PATTERN_TELEPHONE)});

        //手机号校验
        vu.add("phoneNumber", "15757178225","phoneNumber",
                new Rule[]{new Required(), new Length(11), new Numeric(), new Regex(PATTERN_TELEPHONE)});


        String result = vu.validateString();
        if (null != result) {
            System.out.println("参数校验失败,错误信息:" + result);

        }
    }
}
